package transactionExchange.pkg;
//import com.google.appengine.api.datastore.Entity;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;

/**
 * User: Omar A. AlSadoon
 * Date: 10/3/11
 * Time: 12:00 PM
 */
public class Email {

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

    public Email(String to,String bookName,String bookAuthor) {//Construct to send notification email containing "To" and Book info;


        String msgBody = "You've successfully Added the book "+bookName+" written by "+bookAuthor+".";

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("omar.sadoon@gmail.com", "GTUG Book List App"));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            msg.setSubject("Your Books have been added successfully");
            msg.setText(msgBody);
            Transport.send(msg);

        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


    }


    public Email() {//Construct to send report email as a cron job;

        StringBuffer msgBody = new StringBuffer();
        Message msg = new MimeMessage(session);
    	try {
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress("omar.sadoon@gmail.com"));
    	    msg.setSubject("Subject");
        	msg.setFrom(new InternetAddress("omar.sadoon@gmail.com", "Application"));

            msgBody.append("App\n");

            /*  for (Entity result : Book.queryData("Date")) { //Prepare query sorted ascending by Time.

                    msgBody.append(result.getProperty("Book Name"));
                    msgBody.append("\t");
                    msgBody.append(result.getProperty("User"));
                    msgBody.append("\n");
                     }
            */
            msgBody.append("\n");
            msg.setText(msgBody.toString());
    	    Transport.send(msg);

        }catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}
